package com.company.controller;

import com.company.constant.MessageConstant;
import com.company.constant.RedisConstant;
import com.company.entity.PageResult;
import com.company.entity.QueryPageBean;
import com.company.entity.Result;
import com.company.pojo.CheckGroup;
import com.company.pojo.CheckItem;
import com.company.pojo.Setmeal;
import com.company.service.CheckItemService;
import com.company.service.SetMealService;
import com.company.util.QiniuUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * @author fu xiao liu
 * @date 2021-08-06 15:08
 * 套餐管理 增删改查
 */
@RestController
@RequestMapping("/setmeal")
public class SetMealController {

    @DubboReference //查询服务
    private SetMealService setmealService;

    @Autowired
    private JedisPool jedisPool;


    /**
     * @date 展示检查组信息  (用于增加套餐时显示检查组)
     */
    @RequestMapping("/findAll")
    public Result findAll() {
        List<CheckGroup> checkGroupList = null;
        try {
            checkGroupList = setmealService.findAllFromCheckGroup();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroupList);
    }


    /**
     * @date  分页查询
     * @param queryPageBean 前端参数
     * @return PageResult  分页结果封装对象
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        System.out.println("-------------分页查询---------------");
        PageResult pageResult = setmealService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }


    /**
     * @date  新增套餐
     * @param setmeal  套餐信息
     * @param checkgroupIds 套餐绑定的检查组信息
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        try {
            setmealService.add(setmeal, checkgroupIds);
        } catch (Exception e) {
            //新增套餐失败
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
        //新增套餐成功
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    /**
     * @date 图片上传
     * @param imgFile 前端图片
     */
    @RequestMapping("/upload.do")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {
        try {
            //获取原始⽂件名
            String originalFilename = imgFile.getOriginalFilename();
            int lastIndexOf = originalFilename.lastIndexOf(".");

            //获取⽂件后缀
            String suffix = originalFilename.substring(lastIndexOf - 1);

            //使⽤UUID随机产⽣⽂件名称，防⽌同名⽂件覆盖
            String fileName = UUID.randomUUID().toString() + suffix;
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);

            //图⽚上传成功
            Result result = new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS);

            //将上传图片名存入到Redis，基于Redis的set集合
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
            result.setData(fileName);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    /**
     * @date 删除套餐
     * @param id
     * @return com.company.entity.Result
     */
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            setmealService.delete(id);
        } catch (Exception e) {
            return new Result(false, MessageConstant.DELETE_SETMEAL_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
    }

    /**
     * @date  根据ID查询套餐
     * @param id ID
     * @return com.company.entity.Result
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        Setmeal setmeal=null;
        try {
            setmeal =  setmealService.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
        return  new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
    }

    /**
     * @date  根据ID查询对应套餐绑定的检查组（ID列表）
     * @param id 套餐ID
     * @return com.company.entity.Result
     */
    @RequestMapping("/findcheckgroupIdsBySetmealId")
    public Result findCheckItemIdsByCheckGroupId(Integer id) {
        try {
            List<Integer> checkGroupIds = setmealService.findCheckGroupIdBySetMeal(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroupIds);//查询成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     * @date  套餐修改
     * @param setmeal
     * @param checkgroupIds
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        try {
            System.out.println(setmeal.toString() + "  " + Arrays.toString(checkgroupIds));
            setmealService.edit(setmeal,checkgroupIds);
            return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }
}